﻿using BLL;
using Function;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class Index : System.Web.UI.Page
    {
        protected ST_InvestorBLL investorbll = new ST_InvestorBLL();
        protected string randomNum;
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Random random = new Random(22 * (int)System.DateTime.Now.Ticks);
            randomNum = random.Next(126).ToString();
            if (!IsPostBack)
            {
                var url = FormsAuthentication.GetRedirectUrl(Context.User.Identity.Name, false);
                //判断从内部跳转至登录页
                if (url.Contains("Module")) 
                {
                    MessageBox box = new MessageBox();
                    box.AlertBox(this, "请求无效 请重新登录", "Index.aspx", true);
                    return;
                }
            }
        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            //初始化label
            div_msg.Visible = false;
            var name = username2.Value;
            var pwd = password2.Value;
            var code = captcha2.Value;

            if (name.IsEmpty())
            {
                div_msg.InnerHtml = "请输入用户名";
                div_msg.Visible = true;
                return;
            }
            if (pwd.IsEmpty())
            {
                div_msg.InnerHtml = "请输入登陆密码";
                div_msg.Visible = true;
                return;
            }

            if (code.IsEmpty())
            {
                div_msg.InnerHtml = "请输入验证码";
                div_msg.Visible = true;
                return;
            }

            if (!investorbll.ExistsByEmail(name))
            {
                div_msg.InnerHtml = "用户名[" + name + "]不存在";
                div_msg.Visible = true;
                return;
            }
            if (Session["ValidateCode"].ToString() != code)
            {
                div_msg.InnerHtml = "验证码输入错误";
                div_msg.Visible = true;
                return;
            }
            var credit = investorbll.GetModel(name, Method.GetMD5(pwd, Function.Method.MD5EncryptBit.bit32));
            if (credit == null)
            {
                div_msg.InnerHtml = "用户名和密码不正确";
                div_msg.Visible = true;
                return;
            }
            if (credit.State == 1)
            {
                FormsAuthentication.SetAuthCookie(name + "|" + credit.ID, true);
                //Response.Redirect("MainWeb.aspx?p=" + Server.UrlEncode(Method.Encode(credit.Name + "(" + name + ")")) + "&uid=" + credit.ID);

                JL_InvestorLoginLog loginModel = new JL_InvestorLoginLog();
                loginModel.InvestorID = credit.ID;
                loginModel.LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                loginModel.LogIP = Method.ResponseMyIP();

                new JL_InvestorLoginLogBLL().Add(loginModel);

                Response.Redirect("MainWeb.aspx");
            }
            else if (credit.State == 2)
            {
                div_msg.InnerHtml = "该用户已注销";
                div_msg.Visible = true;
                return;
            }
            else
            {
                div_msg.InnerHtml = "非法用户";
                div_msg.Visible = true;
                return;
            }
        }
    }
}